<template>
    <view class="action-modal" v-if="visible">
        <!-- 遮罩层 -->
        <view class="mask" @tap="handleClose" :animation="maskAnimation"></view>

        <!-- 弹窗内容 -->
        <view class="modal-content" :animation="contentAnimation">
            <!-- 修改头部区域 -->
            <view class="modal-header">
                <text class="header-text">技巧提醒</text>
                <view class="close-btn" @tap="handleClose">
                    <text class="close-icon">×</text>
                </view>
            </view>

            <!-- 图标 -->
            <view class="modal-icon">
                <image class="icon-image" :src="icon" mode="aspectFit" />
            </view>

            <!-- 标题 -->
            <view class="modal-title">{{ title }}</view>

            <!-- 内容 -->
            <view class="modal-desc">{{ content }}</view>

            <!-- 按钮 -->
            <view class="modal-btn" @tap="handleConfirm">
                {{ confirmText }}
            </view>
        </view>
    </view>
</template>

<script setup>
import { nextTick, ref, watch } from 'vue';

const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    },
    icon: {
        type: String,
        default: ''
    },
    title: {
        type: String,
        default: ''
    },
    content: {
        type: String,
        default: ''
    },
    confirmText: {
        type: String,
        default: '我知道了'
    }
})

const emit = defineEmits(['close', 'confirm'])

// 动画数据
const maskAnimation = ref(null)
const contentAnimation = ref(null)

// 创建动画实例
const maskAni = uni.createAnimation({
    duration: 300,
    timingFunction: 'ease-out',
    delay: 0
})

const contentAni = uni.createAnimation({
    duration: 300,
    timingFunction: 'ease-out',
    delay: 0
})

// 显示动画
const showAnimation = () => {
    // 先设置初始状态
    maskAni.opacity(0).step({ duration: 0 })
    contentAni.translateY('100%').step({ duration: 0 })

    maskAnimation.value = maskAni.export()
    contentAnimation.value = contentAni.export()

    // 延迟一帧后执行显示动画
    setTimeout(() => {
        maskAni.opacity(1).step()
        contentAni.translateY(0).step()

        maskAnimation.value = maskAni.export()
        contentAnimation.value = contentAni.export()
    }, 50)
}

// 隐藏动画
const hideAnimation = () => {
    // 遮罩层动画
    maskAni.opacity(0).step()
    maskAnimation.value = maskAni.export()

    // 内容动画
    contentAni.translateY('100%').step()
    contentAnimation.value = contentAni.export()
}

// 监听visible变化
watch(() => props.visible, (newVal) => {
    if (newVal) {
        nextTick(() => {
            showAnimation()
        })
    }
}, { immediate: true })

// 处理关闭
const handleClose = () => {
    hideAnimation()
    setTimeout(() => {
        emit('close')
    }, 300)
}

const handleConfirm = () => {
    emit('confirm')
    handleClose()
}
</script>

<style lang="scss" scoped>
.action-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999;

    // 遮罩层
    .mask {
        position: absolute;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
    }

    // 弹窗内容
    .modal-content {
        position: absolute;
        left: 80rpx;
        right: 80rpx;
        bottom: 40rpx;
        background: #fff;
        border-radius: 24rpx;
        padding: 20rpx;
        transform: translateY(100%);

        // 修改头部样式
        .modal-header {
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            margin-bottom: 40rpx;

            .close-btn {
                position: absolute;
                right: 0;
                top: 50%;
                transform: translateY(-50%);
                width: 60rpx;
                height: 60rpx;
                border-radius: 30rpx;
                background: #F5F5F5;
                display: flex;
                align-items: center;
                justify-content: center;

                .close-icon {
                    font-size: 32rpx;
                    color: #999;
                    line-height: 1;
                    transform: translateY(-1rpx);
                }
            }

            .header-text {
                font-size: 28rpx;
                color: #999;
            }
        }

        // 图标
        .modal-icon {
            display: flex;
            justify-content: center;
            margin-bottom: 40rpx;

            .icon-image {
                width: 160rpx;
                height: 160rpx;
            }
        }

        // 标题
        .modal-title {
            font-size: 36rpx;
            font-weight: 500;
            color: #333;
            text-align: center;
            margin-bottom: 24rpx;
        }

        // 内容
        .modal-desc {
            font-size: 28rpx;
            color: #666;
            text-align: center;
            margin-bottom: 48rpx;
            line-height: 1.5;
            padding: 0 40rpx;
        }

        // 按钮
        .modal-btn {
            width: auto;
            margin: 0 80rpx;
            background: #4080FF;
            color: #fff;
            height: 88rpx;
            border-radius: 44rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 32rpx;
            padding: 0 60rpx;

            &:active {
                opacity: 0.9;
            }
        }
    }
}
</style>